import $util from "@/utils/index.js"
import $api from "@/api/index.js"
import $store from "@/store/index.js"
import {
	req
} from '@/utils/req.js';
export default {
	state: {
		autograph: '',
		userInfo: {},
		appLogin: '',
		loginType: '',
		loginPage: '',
		isGzhLogin: false,
		isShowLogin: false,
		isAppLogin: false,
		locationChange: false,
		locationChangeUnix: 0,
		commonOptions: {},
		location: {
			lat: '',
			lng: '',
			address: '',
			is_util_loca: 0
		},
		locaRefuse: false,
		changeAddr: false,
		isShowAuth: true,
		userPageType: 1, // 1用户，2向导
		coachInfo: {
			is_work: 0
		},
		userCoachStatus: {},
		useChooseLocation: false,
		changeOnAddr: {},
		noChangeLoca: {
			noloca: false
		},
		haveShieldOper: 0,
		personVerifyUrl: '',
		fddExtsign: '',
		oldMovable: {
			x: 0,
			y: 0
		},
		userPrivacySetting: {},
		resolvePrivacy: {},
		wecom_staff: '',
		isPhone: false,
	},
	mutations: {
		//更新内容
		async updateUserItem(state, item) {
			let {
				key,
				val
			} = item
			if (key == 'userInfo' && val.id) {}

			

			if (['autograph', 'userInfo', 'location', 'appLogin', 'loginType', 'isShowLogin','loginPage', 'isGzhLogin']
				.includes(key)) {
				uni.setStorageSync(key, val)
			}

			
			// #ifdef APP-PLUS
			if (key === 'location') {
				let locaRefuse = !val || (val && !val.lat && !val.lng) ? true : false
				state.locaRefuse = locaRefuse
				uni.setStorageSync('locaRefuse', locaRefuse)
			}
			
			if (key === 'locaRefuse') {
				uni.setStorageSync('locaRefuse', val)
			}
			// #endif

			state[key] = val
		}
	},
	actions: {
		//获取个人信息
		async getUserInfo({
			commit,
			state
		}, param) {
			let data = await $api.user.userInfo()
			commit('updateUserItem', {
				key: 'userInfo',
				val: data
			})
		},
		//获取师傅信息
		async getCoachInfo({
			commit,
			state
		}, param) {
			let data = await $api.user.coachInfoIndex()
			commit('updateUserItem', {
				key: 'coachInfo',
				val: data
			})
		},
		// 获取用户信息
		async getAuthUserProfile({
			commit,
			state
		}, param) {
			let {
				nickName,
				avatarUrl
			} = param
			await $api.user.userUpdate(param)
			let data = Object.assign({}, state.userInfo, {
				nickName,
				avatarUrl
			})
			commit('updateUserItem', {
				key: 'userInfo',
				val: data
			})
		},
		// 获取手机号
		async getAuthPhone({
			commit,
			state
		}, {
			e = {
				detail: {}
			},
			must = false
		} = {}) {
			let {
				encryptedData = '', iv = ''
			} = e.detail;
			let phone = ''
			if (encryptedData && iv) {
				let phone = await $api.user.reportPhone({
					encryptedData,
					iv
				})
				let data = Object.assign({}, state.userInfo, {
					phone
				})
				commit('updateUserItem', {
					key: 'userInfo',
					val: data
				})
				return phone;
			}
		},
		async getPrivacySetting({
			commit,
			state
		}, param) {
			// #ifdef MP-WEIXIN
			wx.getPrivacySetting({
				success: res => {
					// 返回结果为: res = { needAuthorization: true/false, privacyContractName: '《xxx隐私保护指引》' }
					commit('updateUserItem', {
						key: 'userPrivacySetting',
						val: res
					})
				},
				fail: () => {},
				complete: () => {}
			})
			// #endif
			// #ifndef MP-WEIXIN
			commit('updateUserItem', {
				key: 'userPrivacySetting',
				val: {
					needAuthorization: false,
					privacyContractName: ''
				}
			})
			// #endif
		},
		// 更新公共参数
		async updateCommonOptions({
			commit,
			state
		}, param) {
			let target = {}
			if (param.scene) {
				let res = await $api.base.getWxCodeData({
					code_id: param.scene
				})
				target = Object.assign({}, state.commonOptions, res.data)
			} else {
				target = Object.assign({}, state.commonOptions, param)
			}
			let data = $util.pick(target, ['id', 'pid', 'coupon_atv_id', 'admin_id', 'channel_id', 'coach_id', 'channel_staff_id'])
			commit('updateUserItem', {
				key: 'commonOptions',
				val: data
			})
			return target
		}
	}
}
